from django import forms
from .models import Banner
from django.utils import timezone


# 横幅管理
class BannerForm(forms.ModelForm):
    class Meta:
        model = Banner
        fields = ['title', 'image', 'link', 'order', 'is_active', 'start_time', 'product']
        widgets = {
            'start_time': forms.DateTimeInput(attrs={'type': 'datetime-local'}),
            'link': forms.URLInput(attrs={'placeholder': 'https://example.com'}),
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # 设置默认时间
        if not self.instance.pk and not self.data.get('start_time'):
            self.initial['start_time'] = timezone.now().strftime('%Y-%m-%dT%H:%M')
